﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace OrganizeYourSearch
{
    /// <summary>
    /// Interaction logic for TagCloud.xaml
    /// </summary>
    public partial class TagCloud : UserControl
    {
        //private long max,min;
        public ObservableCollection<Tag> Tags { get; set; }
        public ObservableCollection<TagInstanceDetail> TagInstances{get;set;}

       /* private static RoutedCommand _openFile;
        public static RoutedCommand OpenFile
        {
            get
            {
                return _openFile;//DeclareCommand(ref _AddBookmark, "AddBookmark");
            }
        }
        */
        public TagCloud()
        {
            try
            {
                InitializeComponent();
                Tags = new ObservableCollection<Tag>();
                TagInstances = new ObservableCollection<TagInstanceDetail>();


                Tags = WPFApplication.Database.GetTags(SearchBox.Text);
                LstTags.ItemsSource = Tags;
                LstTags.DisplayMemberPath = "TagName";
                LstTagInstances.ItemsSource = TagInstances;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            
            
        }

        private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Tags.Clear();
            ObservableCollection<Tag> tempTags = WPFApplication.Database.GetTags(SearchBox.Text);
            foreach (Tag tempTag in tempTags)
            {
                Tags.Add(tempTag);
            }          
        }

        private void LstTags_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Tag selectedTag = LstTags.SelectedValue as Tag;
            TagInstances.Clear();
            if (selectedTag != null)
            {
                ObservableCollection<TagInstanceDetail> tempInstances = WPFApplication.Database.GetTagInstances(selectedTag.TagId);
                foreach (TagInstanceDetail instance in tempInstances)
                {
                    TagInstances.Add(instance);
                }
            }
        }


        private void LstTagInstances_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TagInstanceDetail selectedInstance = LstTagInstances.SelectedValue as TagInstanceDetail;
            if (selectedInstance != null)
            {
                tbDetails.Text = selectedInstance.Description;
                btnDocOpen.IsEnabled = true;
            }
            else
            {
                tbDetails.Text = string.Empty;
                btnDocOpen.IsEnabled = false;
            }
        }


        private void btnDocOpen_Click(object sender, RoutedEventArgs e)
        {
          
            DependencyObject obj = e.Source as DependencyObject;
            MainWindow window;
            
            TagInstanceDetail selectedInstance = LstTagInstances.SelectedValue as TagInstanceDetail;
            string fileName=selectedInstance.Location;
            
            while (true)
            {
                obj = LogicalTreeHelper.GetParent(obj);
                window = obj as MainWindow;
                if (window != null)
                    break;
            }
            window.OpenFileFromGivenPath(fileName);

        }
    }
}
